<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.simpleforum.model.ForumApplication" %>
<%@ page import="com.example.simpleforum.model.Post" %>
<%@ page import="com.example.simpleforum.model.Reply" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html>
<html>
<head>
    <title>个人中心 - 极简论坛</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
    <h1>个人中心</h1>
    <div class="actions">
        <a href="index.jsp" class="btn">返回首页</a>
        <a href="PostListServlet" class="btn">帖子列表</a>
    </div>
    
    <% 
        // 显示更新结果消息
        String updateMessage = (String) request.getAttribute("updateMessage");
        Boolean updateSuccess = (Boolean) request.getAttribute("updateSuccess");
        if (updateMessage != null && !updateMessage.isEmpty()) {
            String messageClass = (updateSuccess != null && updateSuccess) ? "success-message" : "error-message";
    %>
    <div class="<%= messageClass %>">
        <%= updateMessage %>
    </div>
    <% } %>
    
    <div class="user-profile">
        <h2>个人资料</h2>
        
        <%-- 管理员审核区域 --%>
        <% if (request.getAttribute("pendingApplications") != null && !((List<ForumApplication>) request.getAttribute("pendingApplications")).isEmpty()) { %>
        <div class="admin-section">
            <h3>待审核的板块申请</h3>
            <table class="application-table">
                <thead>
                    <tr>
                        <th>申请人</th>
                        <th>板块信息</th>
                        <th>申请理由</th>
                        <th>申请时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <% 
                        List<ForumApplication> pendingApplications = (List<ForumApplication>) request.getAttribute("pendingApplications");
                        for (ForumApplication app : pendingApplications) { 
                    %>
                    <tr>
                        <td>用户ID: <%= app.getApplicantId() %></td>
                        <td>
                            <% if (app.getForumId() != null) { %>
                                管理已有板块ID: <%= app.getForumId() %>
                            <% } else { %>
                                创建新板块: <%= app.getForumName() %>
                                <br>
                                <small>描述: <%= app.getForumDescription() %></small>
                            <% } %>
                        </td>
                        <td><%= app.getReason() %></td>
                        <td><%= app.getApplyTime() %></td>
                        <td>
                            <form action="ForumApplicationReviewServlet" method="post" style="display: inline;">
                                <input type="hidden" name="applicationId" value="<%= app.getId() %>">
                                <input type="hidden" name="action" value="approve">
                                <textarea name="reviewNotes" placeholder="审核备注" rows="2" style="width: 100%;"></textarea>
                                <button type="submit" class="btn">批准</button>
                            </form>
                            <form action="ForumApplicationReviewServlet" method="post" style="display: inline;">
                                <input type="hidden" name="applicationId" value="<%= app.getId() %>">
                                <input type="hidden" name="action" value="reject">
                                <textarea name="reviewNotes" placeholder="拒绝原因" rows="2" style="width: 100%;"></textarea>
                                <button type="submit" class="btn btn-danger">拒绝</button>
                            </form>
                        </td>
                    </tr>
                    <% } %>
                </tbody>
            </table>
        </div>
        <% } %>
        
        <% if (request.getAttribute("adminForums") != null && !((List<Map<String, Object>>) request.getAttribute("adminForums")).isEmpty()) { %>
        <div class="profile-section">
            <h3>我管理的板块</h3>
            <ul>
                <% 
                    List<Map<String, Object>> adminForums = (List<Map<String, Object>>) request.getAttribute("adminForums");
                    for (Map<String, Object> forum : adminForums) { 
                %>
                <li><a href="PostListServlet?forumId=<%= forum.get("id") %>"><%= forum.get("name") %></a></li>
                <% } %>
            </ul>
        </div>
        <% } %>
        
        <% if (request.getAttribute("applications") != null && !((List<ForumApplication>) request.getAttribute("applications")).isEmpty()) { %>
        <div class="profile-section">
            <h3>我的板块申请</h3>
            <table class="application-table">
                <thead>
                    <tr>
                        <th>板块ID</th>
                        <th>申请理由</th>
                        <th>申请时间</th>
                        <th>状态</th>
                        <th>审核备注</th>
                    </tr>
                </thead>
                <tbody>
                    <% 
                        List<ForumApplication> applications = (List<ForumApplication>) request.getAttribute("applications");
                        if (applications != null) {
                            for (ForumApplication app : applications) { 
                    %>
                    <tr>
                        <td><%= app.getForumId() %></td>
                        <td><%= app.getReason() %></td>
                        <td><%= app.getApplyTime() %></td>
                        <td><%= app.getStatusDisplayName() %></td>
                        <td><%= app.getReviewNotes() != null ? app.getReviewNotes() : "" %></td>
                    </tr>
                    <% 
                            } 
                        }
                    %>
                </tbody>
            </table>
        </div>
        <% } %>
        
        <div class="profile-section">
            <a href="ForumApplicationServlet" class="btn">申请创建板块</a>
        </div>
        <div class="profile-section">
            <h3>修改用户名</h3>
            <form action="UserProfileUpdateServlet" method="post">
                <input type="hidden" name="action" value="updateUsername">
                <div class="form-group">
                    <label for="newUsername">新用户名:</label>
                    <input type="text" id="newUsername" name="newUsername" value="${user.username}" required minlength="3" maxlength="20">
                </div>
                <div class="form-actions">
                    <button type="submit" class="btn">保存用户名</button>
                </div>
            </form>
        </div>
        
        <div class="profile-section">
            <h3>修改密码</h3>
            <form action="UserProfileUpdateServlet" method="post">
                <input type="hidden" name="action" value="updatePassword">
                <div class="form-group">
                    <label for="currentPassword">当前密码:</label>
                    <input type="password" id="currentPassword" name="currentPassword" required>
                </div>
                <div class="form-group">
                    <label for="newPassword">新密码:</label>
                    <input type="password" id="newPassword" name="newPassword" required minlength="6" maxlength="20">
                    <div class="password-rules">
                        密码必须：
                        <ul>
                            <li>长度在6-20个字符之间</li>
                            <li>包含以下四种字符中的至少三种：大写字母、小写字母、数字、特殊字符(仅限"_","@","%",".","-")</li>
                        </ul>
                    </div>
                </div>
                <div class="form-group">
                    <label for="confirmPassword">确认新密码:</label>
                    <input type="password" id="confirmPassword" name="confirmPassword" required minlength="6" maxlength="20">
                </div>
                <div class="form-actions">
                    <button type="submit" class="btn">修改密码</button>
                </div>
            </form>
        </div>
    </div>
    
    <div class="user-posts">
        <h2>我的帖子</h2>
        <% 
            List<Post> posts = (List<Post>) request.getAttribute("posts");
            if (posts != null && !posts.isEmpty()) {
                for (Post post : posts) {
        %>
        <div class="post-item">
            <h3><a href="PostDetailServlet?id=<%= post.getId() %>"><%= post.getTitle() %></a></h3>
            <div class="post-meta">
                <span>发布时间: <%= post.getCreateTime() %></span>
                <% if (post.getUpdateTime().after(post.getCreateTime())) { %>
                <span>最后编辑: <%= post.getUpdateTime() %></span>
                <% } %>
            </div>
            <div class="post-actions">
                <a href="PostEditServlet?id=<%= post.getId() %>" class="btn">编辑</a>
                <a href="PostDeleteServlet?id=<%= post.getId() %>" class="btn" onclick="return confirm('确定要删除这篇帖子吗？')">删除</a>
            </div>
        </div>
        <% 
                }
            } else {
        %>
        <p>暂无帖子</p>
        <a href="PostAddServlet" class="btn">发布新帖</a>
        <% } %>
    </div>
    
    <div class="user-replies">
        <h2>我的回复</h2>
        <% 
            List<Reply> replies = (List<Reply>) request.getAttribute("replies");
            if (replies != null && !replies.isEmpty()) {
                for (Reply reply : replies) {
        %>
        <div class="reply-item">
            <div class="reply-meta">
                <span>回复时间: <%= reply.getCreateTime() %></span>
                <% if (reply.getUpdateTime().after(reply.getCreateTime())) { %>
                <span>最后编辑: <%= reply.getUpdateTime() %></span>
                <% } %>
            </div>
            <div class="reply-content">
                <%= reply.getContent() %>
            </div>
            <div class="reply-actions">
                <a href="ReplyEditServlet?id=<%= reply.getId() %>" class="btn">编辑</a>
                <a href="ReplyDeleteServlet?id=<%= reply.getId() %>" class="btn" onclick="return confirm('确定要删除这条回复吗？')">删除</a>
                <a href="PostDetailServlet?id=<%= reply.getPostId() %>" class="btn">查看帖子</a>
            </div>
        </div>
        <% 
                }
            } else {
        %>
        <p>暂无回复</p>
        <% } %>
    </div>
</div>
</body>
</html>